package com.emns.web.zhihui;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.emns.pojo.Lanmu;
import com.emns.pojo.LanmuZiyuan;
import com.emns.pojo.Zhihui;
import com.emns.service.zhihui.ZhihuiService;

import common.DateUtils;
import common.PageInfo;

@Controller
@RequestMapping(value = "/zhihui")
public class ZhuiController {
	@Autowired
	ZhihuiService zs;

	@RequestMapping(value = "/addZhihui", method = RequestMethod.POST)
	/**
	 * 添加智慧
	 * @param model
	 * @param zhihui
	 * @return
	 */
	public String addZhihui(Model model, Zhihui zhihui, String[] lanmu,
			int flanmu) {
		zhihui.settUserUserId(1);
		// 错误
		int error = 0;
		zhihui.setPublishdate(new Date());
		int result = zs.addZhihui(zhihui);
		// 整理所选择的栏目数据
		for (int i = 0; i < lanmu.length; i++) {
			int lmid = Integer.parseInt(lanmu[i]);
			Lanmu lm = zs.getLanmuById(lmid);
			LanmuZiyuan lzy = new LanmuZiyuan();
			lzy.setZiyuanId(zhihui.getZhihuiId());
			lzy.setFirstLanmuId(flanmu);
			lzy.setSecondLanmuId(lm.getId());

			if (zs.addLanmuZiyuan(lzy) <= 0) {
				error++;
			}
		}
		// 整理结束
		if (result > 0 && error == 0) {
			return "redirect:/zhihui/query?nowPage=1";
		} else {
			return "";
		}

	}

	@RequestMapping(value = "/queryLanmu")
	public String queryLanmu(Model model) {

		model.addAttribute("info", zs.getMulu());
		return "/zhihui_add";
	}

	@RequestMapping(value = "/query")
	/**
	 * 查询智慧
	 * @param model
	 * @param nowPage 当前页数
	 * @return
	 */
	public String queryZhihui(Model model, int nowPage,
			@RequestParam(required = false) String zName,
			@RequestParam(required = false) String uName,
			@RequestParam(required = false) String bDate,
			@RequestParam(required = false) String eDate) {
		// HttpServletRequest request = ((ServletRequestAttributes)
		// RequestContextHolder
		// .getRequestAttributes()).getRequest();
		// try {
		// request.setCharacterEncoding("UTF-8");
		// } catch (UnsupportedEncodingException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// String zNames="";
		// if(zName!=null){
		// // try {
		// // byte[]temp=zName.getBytes("ISO-8859-1");
		// // zNames=new String(temp,"UTF-8");
		// // } catch (UnsupportedEncodingException e) {
		// // // TODO Auto-generated catch block
		// // e.printStackTrace();
		// // }
		// }
		Date begin = null;
		Date end = null;
		if (bDate != null && bDate != "") {
			begin = new Date();
			begin = DateUtils.parseDate(bDate, "yyyy-MM-dd");
		}
		if (eDate != null && eDate != "") {
			end = new Date();
			end = DateUtils.parseDate(eDate, "yyyy-MM-dd");
		}
		if (zName == null) {
			zName = "";
		}
		if (uName == null) {
			uName = "";
		}
		PageInfo<Zhihui> page = zs.queryByPram(nowPage, zName, uName, begin,
				end);
		model.addAttribute("page", page);
		model.addAttribute("zName", zName);
		model.addAttribute("bDate", bDate);
		model.addAttribute("eDate", eDate);
		return "/zhihui_manager";

	}

	/**
	 * 删除智慧
	 * 
	 * @param model
	 * @param zid
	 * @return
	 */
	@RequestMapping(value = "/dropZhihui")
	public String dropZhihui(Model model, Integer[] zId, int nowPage,
			@RequestParam(required = false) String zName,
			@RequestParam(required = false) String uName,
			@RequestParam(required = false) String bDate,
			@RequestParam(required = false) String eDate) {

		int result = zs.dropZhihuiById(zId);
		if (result > 0) {
			return "forward:/zhihui/query";
		} else {
			return "";
		}

	}

	/**
	 * 查询智慧和相关资源映射
	 * 
	 * @param model
	 * @param zid
	 * @return
	 */
	@RequestMapping(value = "/queryZhihui")
	public String queryZhihui(Model model, int zid, HttpServletRequest request) {
		// 查询智慧
		Zhihui zhihui = zs.getZhihuiById(zid);
		// 查询资源栏目映射
		List<LanmuZiyuan> lmzy = zs.getLanmuZiyuan(zid);
		ServletContext servletContext = request.getSession()
				.getServletContext();
		List<Lanmu> lanmuList = zs.getSLanmu();

		for (int i = 0; i < lanmuList.size(); i++) {
			Lanmu l = lanmuList.get(i);
			for (int j = 0; j < lmzy.size(); j++) {
				LanmuZiyuan lmz = lmzy.get(j);
				if (l.getId() == lmz.getSecondLanmuId()) {
					l.setSeleted(true);
				}
			}
		}

		model.addAttribute("zhihui", zhihui);
		model.addAttribute("lmzy", lanmuList);
		Integer fLid = lmzy.get(0).getFirstLanmuId();

		model.addAttribute("flid", fLid.toString());
		return "/zhihui_update";
	}

	/**
	 * 修改
	 * 
	 * @param model
	 * @param zid
	 * @return
	 */
	@RequestMapping(value = "/updateZhihui")
	public String updateZhihui(Model model, Zhihui zhihui, String[] lanmu,
			int flanmu) {
		// 查询智慧
		// Zhihui zhihui =zs.getZhihuiById(zhihuis.getZhihuiId());
		int dropResult = zs.deleteZiyuan(zhihui.getZhihuiId());
		// 错误
		int error = 0;
		// zhihui.setPublishdate(new Date());
		int result = zs.updateZhihui(zhihui);
		// 整理所选择的栏目数据
		for (int i = 0; i < lanmu.length; i++) {
			int lmid = Integer.parseInt(lanmu[i]);
			Lanmu lm = zs.getLanmuById(lmid);
			LanmuZiyuan lzy = new LanmuZiyuan();
			lzy.setZiyuanId(zhihui.getZhihuiId());
			lzy.setFirstLanmuId(flanmu);
			lzy.setSecondLanmuId(lm.getId());

			if (zs.addLanmuZiyuan(lzy) <= 0) {
				error++;
			}
		}
		// 整理结束
		if (result > 0 && error == 0) {
			return "redirect:/zhihui/query?nowPage=1";
		} else {
			return "";
		}

	}

	// ///////////////////////////////////////////////////////
	// mobile controller
	// //////////////////////////////////
	@RequestMapping(value = "/addZhihuiByMobile", method = RequestMethod.POST)
	/**
	 * 添加智慧
	 * @param model
	 * @param zhihui
	 * @return
	 */
	public String addZhihuiByMobile(Model model, Zhihui zhihui, int lanmu,
			int flanmu) {
		zhihui.settUserUserId(1);
		// 错误
		int error = 0;
		zhihui.setPublishdate(new Date());
		int result = zs.addZhihui(zhihui);
		// 整理所选择的栏目数据

		Lanmu lm = zs.getLanmuById(lanmu);
		LanmuZiyuan lzy = new LanmuZiyuan();
		lzy.setZiyuanId(zhihui.getZhihuiId());
		lzy.setFirstLanmuId(flanmu);
		lzy.setSecondLanmuId(lm.getId());

		if (zs.addLanmuZiyuan(lzy) <= 0) {
			error++;
		}

		// 整理结束
		if (result > 0 && error == 0) {
			model.addAttribute("errorCode", "0");
			model.addAttribute("errorMessage", "发布成功");
			model.addAttribute("zhihui", null);

			return "";
		} else {
			model.addAttribute("errorCode", "2");
			model.addAttribute("errorMessage", "发布成失败");
			zhihui = null;

			return "";
		}

	}

	
	/**
	 * 查询智慧列表
	 * @param model
	 * @param zhihui
	 * @return
	 */
	@RequestMapping(value = "/queryZhihuiByMobile")
	public String queryZhihuiByMobile(Model model, int slanmu, int flanmu,
			int nowPage) {

		if (nowPage < 1) {
			nowPage = 1;
		}
		PageInfo p = zs.queryZhihuiList(nowPage, flanmu, slanmu);

		if (p != null) {
			model.addAttribute("errorCode", "0");
			model.addAttribute("info", p);
		} else {
			model.addAttribute("errorCode", "4");
			model.addAttribute("errorMessage", "查询有误");
		}
		return "";
	}
	/**
	 * 搜索智慧列表
	 * @param model
	 * @param str
	 * @param nowPage
	 * @return
	 */
	@RequestMapping(value = "/searchZhihuiByMobile")
	public String searchZhihuiByMobile(Model model,@RequestParam(required = false)String str,int nowPage){
		if (nowPage < 1) {
			nowPage = 1;
		}
		if(str==null){
			str="";
		}
		PageInfo p = zs.searchZhihuiList(nowPage, str);

		if (p != null) {
			model.addAttribute("errorCode", "0");
			model.addAttribute("info", p);
		} else {
			model.addAttribute("errorCode", "4");
			model.addAttribute("errorMessage", "查询有误");
		}
		return "";
	}
	/**
	 * 查询智慧详情
	 * @param model
	 * @param zid
	 * @return
	 */
	@RequestMapping(value = "/getZhihuiById")
	public String getZhihuiById(Model model,int zid){
		Zhihui z = zs.getZhihuiById(zid);
		if(z!=null){
			model.addAttribute("errorCode", "0");
			model.addAttribute("info", z);
		}else{
			model.addAttribute("errorCode", "4");
			model.addAttribute("errorMessage", "查询有误");
		}
		return  "";
	}
}
